<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="24">
          <el-form-item :label="$t('申请标题')" prop="actTitle">
            <el-input v-model="form.actTitle" :placeholder="$t('申请标题')" show-word-limit :maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealId">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/deal/home/dealBaseHome',query:{id:form.dealId,companyId:form.companyId}}"
                class="link_a"
              >{{ form.dealName }} </router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('标的公司名称')" prop="companyId">
            <template>
              <router-link
                target="_blank"
                :to="{path:'/company/base/companyBaseHome',query:{companyId:form.companyId}}"
                class="link_a"
              >{{ form.companyName }}</router-link>
            </template>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('委托评估/审计单位')" prop="auditUnit">
            <el-input v-model="form.auditUnit" :placeholder="$t('委托评估/审计单位')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('拟进场时间')" prop="entryTime">
            <DatePicker v-model="form.entryTime" :placeholder="$t('拟进场时间')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('报价最高限价') + $t('金额单位')" prop="priceCeiling">
            <Money v-model="form.priceCeiling" :placeholder="$t('报价最高限价')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('主体类型')" prop="investId">
            <dictSelect
              v-model="form.investType"
              :clearable="true"
              dictionary="main_body_type"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('主体名称')">
            <MainBodySelect
              v-model="form.investId"
              :parameters="form.investType"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('选聘类型')" prop="selectType">
            <el-select v-model="form.selectType" multiple :placeholder="$t('选聘类型')" @change="typeOnSelect">
              <el-option
                v-for="dict in selectTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('项目实施依据')" prop="dealImpl">
            <el-input v-model="form.dealImpl" type="textarea" :placeholder="$t('项目实施依据')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="area-header">
        <span class="area-header-title">{{ $t('拟招标中介机构') }}</span>
      </div>
      <el-table :data="form.dealWfBidIntermediaryList" class="nowrapTable formTable" border>
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('中介机构名称')" align="center" prop="mediationId">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('中介机构名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item
              :prop="'dealWfBidIntermediaryList.' + scope.$index + '.mediationId'"
              :rules="[ { required: true, message: $t('必填信息'), trigger: 'blur' } ]"
            >
              <el-select v-model="scope.row.mediationId" :placeholder="$t('请选取选聘类型后填写')" @change="mediationChangeHandle($event, scope.$index)">
                <el-option
                  v-for="(item, index) in mediationTypeOptions"
                  :key="index"
                  :label="item.mediationName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('中介报价') + $t('金额单位')" align="center" prop="offer">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('中介报价') }}{{ $t('金额单位') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item
              :prop="'dealWfBidIntermediaryList.' + scope.$index + '.offer'"
              :rules="[ { required: true, message: $t('必填信息'), trigger: 'blur' } ]"
            >
              <Money v-model="scope.row.offer" unit="w" :placeholder="$t('中介报价')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('是否中标')" align="center" prop="bid">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('是否中标') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item
              :prop="'dealWfBidIntermediaryList.' + scope.$index + '.bid'"
              :rules="[ { required: true, message: $t('必填信息'), trigger: 'blur' } ]"
            >
              <el-radio-group v-model="scope.row.bid" @change="radioChange($event, scope.$index)">
                <el-radio
                  v-for="dict in sysYesNoOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{ $t(dict.dictLabel) }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column v-if="!otherBtnHide" :label="$t('操作')" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="dealBidIntermediary(scope.$index, scope.row)">{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button v-if="!otherBtnHide" type="primary" icon="el-icon-plus" size="mini" @click="addBidIntermediary">{{ $t("添加") }}</el-button>
      </div>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="deal_wf_intermediary" :business-id="form.fundId" :btn-show="!otherBtnHide" />
    </el-form>
  </div>
</template>

<script>
    import { saveDealWfIntermediary, getDealWfIntermediary } from '@/api/deal/intermediary/dealWfIntermediary'
    import MainBodySelect from '@/components/MainBodySelect/index'
    import { getByWfNodeId } from '@/api/deal/form/dealWfGeneral'
    import { findByTypeList } from '@/api/deal/mediation/mediationBase'
    export default {
      components: {
        MainBodySelect
      },
        data() {
            return {
                otherBtnHide: false,
                // 选聘类型类型字典
                selectTypeOptions: [], // 表单参数
                sysYesNoOptions: [], // 系统是否
                // 表单参数
                form: {
                  dealWfBidIntermediaryList: []
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  actTitle: [
                    { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                  ],
                  selectType: { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                },
              // 中介机构
              mediationTypeOptions: []
            }
        },
        created() {
          this.getDicts('intermediary_type').then(response => {
            this.selectTypeOptions = response.data
          })
          this.getDicts('sys_yes_no').then(response => {
            this.sysYesNoOptions = response.data
          })
        },
      mounted() {
        this.$bus.$emit('async_done')
      },
      methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealWfIntermediary(data.id).then(response => {
                      this.form = response.data
                      if (response.data.selectType) {
                        findByTypeList(response.data.selectType).then(response => {
                          this.mediationTypeOptions = response.data
                        })
                      }
                      this.loading = false
                    })
                } else {
                    getByWfNodeId(data.wfNodeId).then(response => {
                      this.form = response.data
                      this.form.fileList = data.fileList
                      this.form.actTitle = data.actTitle
                      this.loading = false
                    })
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                dealId: undefined,
                companyId: undefined,
                auditUnit: undefined,
                entryTime: undefined,
                priceCeiling: undefined,
                investId: undefined,
                investType: undefined,
                selectType: undefined,
                dealImpl: undefined,
                createBy: undefined,
                updateBy: undefined,
                createTime: undefined,
                updateTime: undefined,
                delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            /** 提交按钮 */
            submitForm: function() {
              return new Promise((resolve, reject) => {
                this.$refs['form'].validate(valid => {
                  if (valid) {
                    this.loading = true
                    saveDealWfIntermediary(this.form).then(response => {
                      if (response.code === 200) {
                        this.msgSuccess(this.$t('保存成功'))
                        resolve(response.data.id)
                        this.loading = false
                      } else {
                        this.msgError(response.msg)
                        this.loading = false
                        reject(response)
                      }
                      this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                  } else {
                    this.locationError(document)
                    reject()
                  }
                })
              })
           },
          // 添加拟招标中介机构
          addBidIntermediary() {
            const row = {
              id: undefined,
              intermediaryId: undefined,
              mediationId: undefined,
              offer: undefined,
              bid: undefined
            }
            if (!(this.form.dealWfBidIntermediaryList instanceof Array)) {
              this.$set(this.form, 'dealWfBidIntermediaryList', [])
            }
            this.$nextTick(() => {
              this.form.dealWfBidIntermediaryList.push(row)
            })
          },
          // 删除拟招标中介机构
          dealBidIntermediary(index) {
            this.form.dealWfBidIntermediaryList.splice(index, 1)
          },
          radioChange(val, index) {
            if (val === 'Y') {
              for (let i = 0; i < this.form.dealWfBidIntermediaryList.length; i++) {
                if (i !== index && this.form.dealWfBidIntermediaryList[i].bid === 'Y') {
                  this.form.dealWfBidIntermediaryList[i].bid = 'N'
                  this.msgInfo(this.$t('只能有一个中介机构中标'))
                  return
                }
              }
            }
          },
          typeOnSelect(val) {
              if (val) {
                findByTypeList(val).then(response => {
                  this.mediationTypeOptions = response.data
                })
              } else {
                this.mediationTypeOptions = []
              }
            this.form.dealWfBidIntermediaryList = []
          },
          mediationChangeHandle(val, index) {
            let count = 0
            this.form.dealWfBidIntermediaryList.forEach((ele) => {
              if (ele.mediationId === val) {
                count++
              }
            })
            if (count > 1) {
              this.$message({
                type: 'warning',
                message: this.$t('同一中介机构只能选择一次')
              })
              this.$set(this.form.dealWfBidIntermediaryList[index], 'mediationId', undefined)
            }
          }
        }
    }
</script>
